﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using XTeam.Core.Interface;
using MongoDB.Driver;
using MongoDB.Driver.Linq;
using MongoDB.Driver.Builders;
using MongoDB.Bson.Serialization;
using System.Reflection;

namespace XTeam.Core.Abstract
{
    public abstract class MongoDBCore : IDbCore
    {

        protected abstract MongoDBContext CreateDb();
        /// <summary>
        /// 查找唯一条
        /// </summary>
        public T Find<T>(Func<T, bool> filter) where T : class, new()
        {
            var table = this.GetTable<T>();
            return table.AsQueryable().SingleOrDefault(filter);
        }

        /// <summary>
        /// 查找分页
        /// </summary>
        public IEnumerable<T> Search<T>(Func<T, bool> filter, int pageIndex, int pageSize, out int totalRows) where T : class,new()
        {
            var table = this.GetTable<T>();
            IEnumerable<T> query = null;
            if (filter != null)
            {
                query = table.AsQueryable().Where(filter);
            }
            else
            {
                query = table.AsQueryable().Select(i => i);
            }
            totalRows = query.Count();
            var resultTable = query.Skip((pageIndex - 1) * pageSize)
                  .Take(pageSize);

            return resultTable;
        }
        /// <summary>
        /// 查找
        /// </summary>
        public IEnumerable<T> Search<T>(Func<T, bool> filter) where T : class, new()
        {
            int totalRows;
            return this.Search<T>(filter, 0, int.MaxValue, out totalRows);

        }
        /// <summary>
        /// 统计数量
        /// </summary>
        public int Count<T>(Func<T, bool> filter)
        {
            int count = 0;
            var table = this.GetTable<T>();
            if (filter == null)
            {
                filter = (i) => { return true; };
            }

            count = table.AsQueryable().Count(filter);
            return count;
        }
        /// <summary>
        /// 更新
        /// </summary>
        public int Update<T>(IList<T> entities, Func<T, object> fileds = null, bool withOut = false) where T : class, new()
        {
            var table = this.GetTable<T>();
            int i = 0;
            var type = typeof(T);
            var objectId = BsonClassMap.LookupClassMap(typeof(T)).IdMemberMap.MemberInfo;
            foreach (var item in entities)
            {
                if (fileds != null)
                {
                    fileds(item);
                }

                table.Save(item);
            }
            return i;
        }
        /// <summary>
        /// 删除
        /// </summary>
        public int Delete<T>(IList<T> entities) where T : class, new()
        {
            int i = 0;
            var table = this.GetTable<T>();
            var type = typeof(T);
            var objectId = BsonClassMap.LookupClassMap(typeof(T)).IdMemberMap.MemberInfo;
            foreach (var item in entities)
            {
                i++;
                object value = type.InvokeMember(objectId.Name, BindingFlags.GetField | BindingFlags.GetProperty, null, item, null);
                table.Remove(Query.EQ("_id", string.Format("{0}", value)));
            }
            return i;
        }
        /// <summary>
        /// 插入
        /// </summary>
        public int Append<T>(IList<T> entities) where T : class, new()
        {
            var table = this.GetTable<T>();

            int i = 0;
            foreach (var item in entities)
            {
                i++;
                var res = table.Insert(item);
            }
            return i;
        }
        /// <summary>
        /// 获取表
        /// </summary>
        private MongoCollection<T> GetTable<T>()
        {
            var db = this.CreateDb();
            return db.GetCollection<T>();
        }
        /// <summary>
        /// 转换为查询
        /// </summary>
        public IQueryable<T> AsQueryable<T>() where T : class, new()
        {
            var table = this.GetTable<T>();
            return table.AsQueryable();

        }



    }
}
